<?php

namespace EasyTiktokSdk\Orders;


use EasyTiktokSdk\Kernel\HttpClient;


class Client extends HttpClient
{

    public function search($page_size = 20, $bodyData = [], $options = [])
    {
        $params = [

        ];

        $body = [

        ];

        return $this->httpPostJson('/order/202309/orders/search');
    }


    // 单推-通知栏消息推送（message/notification/unicast）
    protected $notification_unicast = '/server/v1/message/notification/unicast';


    /**
     * @param string $messageId   消息Id
     * @param string $target      推送目标用户【多个以英文分号(;)分隔，最大1000 个】，可以替代registration_id
     * @return array|object|\Psr\Http\Message\ResponseInterface|string
     * @throws \Exception
     *
     * @deprecated 接口不可用
     */
    public function broadcastNotification($messageId, $target)
    {
        if(!isset($messageId) || empty($messageId)){
            throw new \Exception("Attribute message_id can not be empty!");
        }
        if(!isset($target) || empty($target)){
            throw new \Exception("Attribute alias can not be empty!");
        }

        if (is_array($target)) {
            $target = implode(';', $target);
        }

        $result = array(
            'message_id' => $messageId,
            'target_type' => 1,
            'target_value' => $target,
        );

        return $this->httpPostJson($this->notification_broadcast, $result);
    }

    /**
     * @param array $data
     *
     * @return array
     *
     * @throws \Exception
     */
    protected function formatMessage(array $data = [])
    {
        if(!isset($data['title']) || empty($data['title'])){
            throw new \Exception("Attribute title can not be empty!");
        }
        if(!isset($data['content']) || empty($data['content'])){
            throw new \Exception("Attribute content can not be empty!");
        }

        if(!isset($data['sub_title']) || empty($data['sub_title'])){
            throw new \Exception("Attribute sub_title can not be empty!");
        }

        if(!isset($data['click_action_url']) || empty($data['click_action_url'])){
            throw new \Exception("Attribute click_action_url can not be empty!");
        }

        $result = array(
            'title' => $data['title'],
            'content' => $data['content'],
            'sub_title' => $data['sub_title'],
            'click_action_type' => 5, // 点击动作类型
            'click_action_url' => $data['click_action_url'],
            'channel_id' => '本地通知'
        );

        // 动作参数，打开应用内页或网页时传递给应用或网页【JSON 格式，非必填】，
        // 字符数不能超过4K，示例：{"key1":"value1","key2":"value2"}
        if(is_array($data['action_parameters']) && count($data['action_parameters']) > 0){
            $result['action_parameters'] = json_encode($data['action_parameters']);
        }elseif(is_string($data['action_parameters']) && trim($data['action_parameters']) != ''){
            $result['action_parameters'] = $data['action_parameters'];
        }

        // 应用接收消息到达回执的回调URL
        if(isset($data['call_back_url']) && !empty($data['call_back_url'])) {
            $result['call_back_url'] = $data['call_back_url'];
        }
        // App 开发者自定义回执参数，字数限制50 以内，中英文均以一个计算。
        if(isset($data['call_back_parameter']) && !empty($data['call_back_parameter'])) {
            $result['call_back_parameter'] = $data['call_back_parameter'];
        }

        //show_time_type Int 否0 展示类型(0, “即时”),(1, “定时”)
        //show_start_time Long 否0 定时展示开始时间（根据time_zone 转换成当地时间），时间的毫秒数
        //show_end_time Long 否0 定时展示结束时间（根据time_zone 转换成当地时间），时间的毫秒数
        if(isset($data['show_time_type']) && 1 == $data['show_time_type']) {
            $result['show_time_type'] = $data['show_time_type'];
            if (empty($data['show_start_time']) || empty($data['show_end_time'])) {
                throw new \Exception("Attribute show_start_time and show_end_time can not be empty!");
            }
            $result['show_start_time'] = $data['show_start_time'];
            $result['show_end_time'] = $data['show_end_time'];
        }


        return $result;
    }

}